package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.api.subject.entity.dto.PracticeSubjectDTO;
import com.jingdianjichi.api.subject.entity.vo.PracticeSubjectVO;
import com.jingdianjichi.subject.application.comvert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.application.comvert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.comvert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectDomainService;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目模块
 */

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectDomainService subjectDomainService;

    /**
     * 新增题目
     *
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "分类id不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");


            SubjectInfoBO subjectInfoBO = getSubjectInfoBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerDTOConverter.INSTANCE.convertListDTOToInfoBO(subjectInfoDTO.getOptionList());

            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectDomainService.add(subjectInfoBO);
            return Result.ok("新增成功");
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     *
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签Id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类Id不能为空");


            SubjectInfoBO subjectInfoBO = getSubjectInfoBO(subjectInfoDTO);

            PageResult<SubjectInfoBO> boPageResult=subjectDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("查询题目列表失败");
        }
    }

    /**
     * 查询题目信息
     *
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> getSubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目Id不能为空");


            SubjectInfoBO subjectInfoBO = getSubjectInfoBO(subjectInfoDTO);

            SubjectInfoBO boResult=subjectDomainService.getSubjectInfo(subjectInfoBO);
            SubjectInfoDTO result = SubjectInfoDTOConverter.INSTANCE.convertBOToInfoDTO(boResult);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目信息失败");
        }
    }



    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()), "关键词不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToInfoBO(subjectInfoDTO);
            PageResult<SubjectInfoEs> result = subjectDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }



    /**
     * 获取题目贡献榜
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> subjectInfoBOList = subjectDomainService.getContributeList();
            List<SubjectInfoDTO> result = SubjectInfoDTOConverter.INSTANCE.convertBOToInfoListDTO(subjectInfoBOList);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }

    /**
     * 获取题目详情
     * @param practiceSubjectDTO
     * @return
     */
    @PostMapping("/getPracticeSubject")
    public PracticeSubjectVO getPracticeSubject(@RequestBody PracticeSubjectDTO practiceSubjectDTO){
        try {
            PracticeSubjectVO practiceSubjectVO = subjectDomainService.getPracticeSubject(practiceSubjectDTO);
            return practiceSubjectVO;
        } catch (Exception e) {
            log.error("SubjectCategoryController.getPracticeSubject.error:{}", e.getMessage(), e);
            return null;
        }
    }



    /**
     * subjectInfoDTO转换subjectInfoBO
     *
     * @param subjectInfoDTO
     * @return
     */
    private SubjectInfoBO getSubjectInfoBO(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        return SubjectInfoDTOConverter.INSTANCE.convertDtoToInfoBO(subjectInfoDTO);
    }
}
